package net.adetech.crj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.TemplateData;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import net.adetech.crj.biz.ApiUtil;
import net.adetech.crj.dao.*;
import net.adetech.crj.dao.Dictionary;
import net.adetech.crj.dao.Queue;
import net.adetech.crj.entity.QueueInfo;
import net.adetech.crj.utils.Md5Util;
import net.adetech.crj.utils.SerializeUtil;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.crj.utils.entity.ServiceResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

public class CrjController extends Controller {

    private static final String SETTING_TYPE_TIME_LIMIT_1 = "1";
    private static final String SETTING_TYPE_TIME_LIMIT_2 = "2";
    private static final String SETTING_TYPE_MACHINE_PASSWORD = "3";

    private static final String QUEUE_STATE_WAIT = "1";
    private static final String QUEUE_STATE_PERFORM = "2";
    private static final String QUEUE_STATE_COMPLETE = "3";
    private static final String QUEUE_STATE_PASS = "4";
    private static final String QUEUE_STATE_HANGUG = "5";
    private static final String QUEUE_STATE_NEXT = "6";

    private static Map<String, String> serviceNameMap = new HashMap<>();
    private static Map<String, String> serviceZoneMap = new HashMap<>();
    private static Map<String, String> serviceQueueMap = new HashMap<>();
    private static Map<String, String> machineIdMap = new HashMap<>();
    private static Map<String, String> machineIpMap = new HashMap<>();


    private static String PHOTO_MESSAGE_TEMPLATE = PropKit.get("PHOTO_MESSAGE_TEMPLATE");
    private static String PHOTO_MESSAGE_TEMPLATE_VIP = PropKit.get("PHOTO_MESSAGE_TEMPLATE_VIP");
    private static String SERVICE_MESSAGE_TEMPLATE = PropKit.get("SERVICE_MESSAGE_TEMPLATE");
    private static String SERVICE_MESSAGE_TEMPLATE_VIP = PropKit.get("SERVICE_MESSAGE_TEMPLATE_VIP");

    private static String CALL_TEMPLATE = PropKit.get("CALL_TEMPLATE");
    private static String RECOVER_TEMPLATE = PropKit.get("RECOVER_TEMPLATE");
    private static String RECOVER_TEMPLATE_VIP = PropKit.get("RECOVER_TEMPLATE_VIP");
    private static String HANGUP_TEMPLATE = PropKit.get("HANGUP_TEMPLATE");


    private static final String WEB_URL = PropKit.get("WEB_URL");


    static {
        serviceNameMap.put("Z1", "拍照");
        serviceNameMap.put("VZ1", "拍照");
        serviceNameMap.put("A1", "港澳通行证");
        serviceNameMap.put("VA1", "港澳通行证");
        serviceNameMap.put("A2", "护照");
        serviceNameMap.put("VA2", "护照");
        serviceNameMap.put("B1", "签证证件办理");
        serviceNameMap.put("VB1", "签证证件办理");
        serviceNameMap.put("B2", "回大陆定居");
        serviceNameMap.put("VB2", "回大陆定居");
        serviceNameMap.put("C1", "国家工作人员备案");
        serviceNameMap.put("VC1", "国家工作人员备案");
        serviceNameMap.put("C2", "商务备案");
        serviceNameMap.put("VC2", "商务备案");
        serviceNameMap.put("C3", "查询出入境记录");
        serviceNameMap.put("VC3", "查询出入境记录");
        serviceNameMap.put("C4", "中介机构");
        serviceNameMap.put("VC4", "中介机构");
        serviceNameMap.put("C5", "E境通业务");
        serviceNameMap.put("VC5", "E境通业务");
        serviceNameMap.put("C6", "其他涉外业务");
        serviceNameMap.put("VC6", "其他涉外业务");


        serviceZoneMap.put("Z1", "Z");
        serviceZoneMap.put("VZ1", "Z");
        serviceZoneMap.put("A1", "A");
        serviceZoneMap.put("VA1", "A");
        serviceZoneMap.put("A2", "A");
        serviceZoneMap.put("VA2", "A");
        serviceZoneMap.put("B1", "B");
        serviceZoneMap.put("VB1", "B");
        serviceZoneMap.put("B2", "B");
        serviceZoneMap.put("VB2", "B");
        serviceZoneMap.put("C1", "C");
        serviceZoneMap.put("VC1", "C");
        serviceZoneMap.put("C2", "C");
        serviceZoneMap.put("VC2", "C");
        serviceZoneMap.put("C3", "C");
        serviceZoneMap.put("VC3", "C");
        serviceZoneMap.put("C4", "C");
        serviceZoneMap.put("VC4", "C");
        serviceZoneMap.put("C5", "C");
        serviceZoneMap.put("VC5", "C");
        serviceZoneMap.put("C6", "C");
        serviceZoneMap.put("VC6", "C");

        serviceQueueMap.put("Z1", "Z");
        serviceQueueMap.put("VZ1", "VZ");
        serviceQueueMap.put("A1", "A");
        serviceQueueMap.put("VA1", "VA");
        serviceQueueMap.put("A2", "A");
        serviceQueueMap.put("VA2", "VA");
        serviceQueueMap.put("B1", "B");
        serviceQueueMap.put("VB1", "VB");
        serviceQueueMap.put("B2", "B");
        serviceQueueMap.put("VB2", "VB");
        serviceQueueMap.put("C1", "C");
        serviceQueueMap.put("VC1", "VC");
        serviceQueueMap.put("C2", "C");
        serviceQueueMap.put("VC2", "VC");
        serviceQueueMap.put("C3", "C");
        serviceQueueMap.put("VC3", "VC");
        serviceQueueMap.put("C4", "C");
        serviceQueueMap.put("VC4", "VC");
        serviceQueueMap.put("C5", "D");
        serviceQueueMap.put("VC5", "VD");
        serviceQueueMap.put("C6", "D");
        serviceQueueMap.put("VC6", "VD");


        machineIdMap.put("1","2");
        machineIdMap.put("2","2");
        machineIdMap.put("3","2");
        machineIdMap.put("4","2");
        machineIdMap.put("5","3");
        machineIdMap.put("6","3");
        machineIdMap.put("7","3");
        machineIdMap.put("8","3");

        machineIpMap.put("1","");
        machineIpMap.put("2","192.168.2.166");
        machineIpMap.put("3","");

    }


    public void demo() {
        Date start = new Date();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("startTime", start);
        ServiceResponse response = new ServiceUtil().call("abc.123", params);
        Date end = (Date) response.getResults().get("end");
        renderText("延迟:" + (end.getTime() - start.getTime()) + "ms");
    }

    /***
     * 登录接口
     */
    public void login() {
        HttpServletRequest request = this.getRequest();
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("pwd");
        password = Md5Util.getPwd(password);
        Account account = Account.getAccountByLoginName(loginName);
        Machine machine = Machine.dao.findById("1");
        String machineState = machine.getStr("STATE");
        JSONObject result = new JSONObject();
        if (account != null) {
            if (account.getStr("PASSWORD").equals(password)) {
                result.put("result", "ok");
                JSONObject accountJson = JSON.parseObject(account.toJson());
                result.put("account", accountJson);
                result.put("mState", machineState);
            } else {
                result.put("result", "error");
            }

        } else {
            result.put("result", "null");
        }
        renderJson(result.toJSONString());
    }


    public void modifyPwd() {
        HttpServletRequest request = this.getRequest();
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("pwd");
        String newPassword = request.getParameter("newPwd");
        password = Md5Util.getPwd(password);
        newPassword = Md5Util.getPwd(newPassword);
        Account account = Account.getAccountByLoginName(loginName);
        JSONObject result = new JSONObject();
        if (account != null) {
            if (account.getStr("PASSWORD").equals(password)) {
                account.set("PASSWORD", newPassword).update();
                result.put("result", "ok");
                JSONObject accountJson = JSON.parseObject(account.toJson());
                result.put("account", accountJson);
            } else {
                result.put("result", "error");
            }

        } else {
            result.put("result", "null");

        }

        renderJson(result.toJSONString());
    }


    public void setting() {

        HttpServletRequest request = this.getRequest();
        String type = request.getParameter("type");
        String value = request.getParameter("value");
        String dbType = "";
        boolean ok = true;
        if ("tl1".equals(type)) {
            dbType = SETTING_TYPE_TIME_LIMIT_1;
            int tl1 = -1;
            try {
                tl1 = Integer.parseInt(value);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (tl1 < 0) {
                ok = false;
            }
        } else if ("tl2".equals(type)) {
            dbType = SETTING_TYPE_TIME_LIMIT_2;
            int tl2 = -1;
            try {
                tl2 = Integer.parseInt(value);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (tl2 < 0) {
                ok = false;
            }
        } else if ("pwd".equals(type)) {
            value = Md5Util.getPwd(value);
        } else {
            ok = false;
        }
        if (ok) {
            Dictionary.dao.findById(dbType).set("DICT_VALUE", value).update();
            renderJson("{\"result\":\"ok\"}");
        } else {
            renderJson("{\"result\":\"error\"}");
        }
    }


    public void getSetting() {

        HttpServletRequest request = this.getRequest();
        String type = request.getParameter("type");
        String dbType = null;
        if ("tl1".equals(type)) {
            dbType = SETTING_TYPE_TIME_LIMIT_1;
        } else if ("tl2".equals(type)) {
            dbType = SETTING_TYPE_TIME_LIMIT_2;
        }

        if ("pwd".equals(type)) {
            dbType = SETTING_TYPE_MACHINE_PASSWORD;
        }

        if (dbType == null) {
            renderJson("{\"result\":\"error\"}");
        } else {
            Dictionary dictionary = Dictionary.dao.findById(dbType);
            renderJson(dictionary.toJson());
        }

    }


    public void getAllQueuing() {
        List<Queue> queueList = Queue.getTodayQueueList();
        QueueInfo infoZ = new QueueInfo();
        QueueInfo infoA = new QueueInfo();
        QueueInfo infoB = new QueueInfo();
        QueueInfo infoC = new QueueInfo();

        for (Queue q : queueList) {
            String zone = q.getStr("SERVICE_ZONE");
            int state = Integer.parseInt(q.getStr("STATE"));
            QueueInfo queueInfo = null;
            if ("Z".equals(zone)) {
                queueInfo = infoZ;
            } else if ("A".equals(zone)) {
                queueInfo = infoA;
            } else if ("B".equals(zone)) {
                queueInfo = infoB;
            } else if ("C".equals(zone)) {
                queueInfo = infoC;
            }

            queueInfo.setSum(queueInfo.getSum()+1);
            switch (state) {
                case 1:
                    queueInfo.setStateCount1(queueInfo.getStateCount1()+1);
                    break;
                case 2:
                    queueInfo.setStateCount2(queueInfo.getStateCount2()+1);
                    break;
                case 3:
                case 6:
                    queueInfo.setStateCount3(queueInfo.getStateCount3()+1);
                    break;
                case 4:
                    queueInfo.setStateCount4(queueInfo.getStateCount4()+1);
                    break;
                case 5:
                    queueInfo.setStateCount5(queueInfo.getStateCount5()+1);
                    break;

            }
        }
        JSONObject result = new JSONObject();
        JSONObject jsonInfoA = JSONObject.parseObject(JSON.toJSONString(infoA));
        JSONObject jsonInfoB = JSONObject.parseObject(JSON.toJSONString(infoB));
        JSONObject jsonInfoC = JSONObject.parseObject(JSON.toJSONString(infoC));
        JSONObject jsonInfoZ = JSONObject.parseObject(JSON.toJSONString(infoZ));
        result.put("queue_a",jsonInfoA);
        result.put("queue_b",jsonInfoB);
        result.put("queue_c",jsonInfoC);
        result.put("queue_z",jsonInfoZ);
        renderJson(result.toJSONString());

    }


    public void getQueuing() {
        HttpServletRequest request = this.getRequest();
        String zone = request.getParameter("zone");
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String numberState = request.getParameter("state");
        List<Queue> queueList = Queue.dao.find("select * from CRJ_QUEUE where DAY_ID = ? and SERVICE_ZONE = ? order by QUEUE_ID", dayId, zone);
        int sum = 0;
        int wait = 0;
        int perform = 0;
        int complete = 0;
        int hungup = 0;
        JSONObject result = new JSONObject();
        JSONArray queueArray = new JSONArray();
        for (Queue q : queueList) {

            String serviceType = q.get("SERVICE_TYPE");
            String state = q.get("STATE");
            if (numberState == null || numberState.indexOf(state) > -1) {
                if (serviceType.indexOf("V") == 0) {

                    String jsonString = q.toJson();
                    JSONObject qJson = JSONObject.parseObject(jsonString);
                    qJson.put("QUEUE_TYPE", "VIP");
                    qJson.put("SERVICE_NAME", serviceNameMap.get(serviceType));
                    String realName = q.getStr("REAL_NAME");
                    String idNumber = q.getStr("IDENTITY_NUMBER");
                    String wxName = q.getStr("WX_NAME");
                    realName = SerializeUtil.decodeBase64(realName);
                    idNumber = SerializeUtil.decodeBase64(idNumber);
                    wxName = SerializeUtil.decodeBase64(wxName);

                    qJson.put("REAL_NAME", realName);
                    qJson.put("IDENTITY_NUMBER", idNumber);
                    qJson.put("WX_NAME", wxName);

                    queueArray.add(qJson);

                }
            }
            sum++;
            if (QUEUE_STATE_WAIT.equals(state)) {
                wait++;
            } else if (QUEUE_STATE_PERFORM.equals(state)) {
                perform++;
            } else if (QUEUE_STATE_COMPLETE.equals(state)) {
                complete++;
            } else if (QUEUE_STATE_HANGUG.equals(state)) {
                hungup++;
            }
        }

        for (Queue q : queueList) {
            String serviceType = q.get("SERVICE_TYPE");
            String state = q.get("STATE");
            if (numberState == null || numberState.indexOf(state) > -1) {
                if (serviceType.indexOf("V") != 0) {
                    String jsonString = q.toJson();
                    JSONObject qJson = JSONObject.parseObject(jsonString);
                    qJson.put("QUEUE_TYPE", "NORMAL");
                    qJson.put("SERVICE_NAME", serviceNameMap.get(serviceType));

                    String realName = q.getStr("REAL_NAME");
                    String idNumber = q.getStr("IDENTITY_NUMBER");
                    String wxName = q.getStr("WX_NAME");
                    realName = SerializeUtil.decodeBase64(realName);
                    idNumber = SerializeUtil.decodeBase64(idNumber);
                    wxName = SerializeUtil.decodeBase64(wxName);

                    qJson.put("REAL_NAME", realName);
                    qJson.put("IDENTITY_NUMBER", idNumber);
                    qJson.put("WX_NAME", wxName);

                    queueArray.add(qJson);
                }
            }
        }
        if ("Z".equals(zone)) {
            try {
                JSONObject queueJson = ApiUtil.getPhotoQueue();
                String nextNumber = queueJson.getJSONArray("ResBody").getJSONObject(0).getString("next_number");
                result.put("NEXT_NUMBER", nextNumber);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        result.put("SUM", sum);
        result.put("COMPLETE", complete);
        result.put("WAIT", wait);
        result.put("PERFORM", perform);
        result.put("HUNGUP", hungup);
        result.put("QUEUE_LIST", queueArray);
        renderJson(result.toJSONString());
    }

    public void getMachineList() {
        List<Machine> machineList = Machine.dao.find("select * from CRJ_MACHINE");
        JSONArray result = new JSONArray();
        for (Machine m : machineList
                ) {
            result.add(JSONObject.parse(m.toJson()));
        }
        renderJson(result.toJSONString());

    }

    public void setMachine() {
        HttpServletRequest request = this.getRequest();
        String mId = request.getParameter("mId");
        String op = request.getParameter("op");
        String param = request.getParameter("p");
        new MachineOperation().set("OP_ID", "SEQ_OP_ID.nextval").set("MACHINE_ID", mId).set("OP_TYPE", op).set("OP_PARAMS", param).set("STATE", "0").save();

        renderJson("{\"result\":\"ok\"}");
    }

    public void getMachineOp() {
        HttpServletRequest request = this.getRequest();
        //String mId = request.getParameter("mId");
        List<MachineOperation> machineOperationList = MachineOperation.dao.find("select * from CRJ_MACHINE_OPERATION where STATE = '0'");
        //JSONArray result = new JSONArray();
        JSONArray result = new JSONArray();
        for (MachineOperation op : machineOperationList
                ) {
            //result.add(JSONObject.parse(op.toJson()));
            op.set("STATE", "1").update();
            JSONObject opJson = JSONObject.parseObject(op.toJson());

            String mId = op.getStr("MACHINE_ID");
            String mIp = machineIpMap.get(mId);
            opJson.put("IP",mIp);

            if ("3".equals(op.getStr("OP_TYPE"))) {
                String queueNumber = op.getStr("OP_PARAMS");

                String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
                List<Queue> queueList = Queue.dao.find("select * from CRJ_QUEUE where QUEUE_NUMBER = ? and DAY_ID = ?", queueNumber, dayId);
                if (queueList != null && queueList.size() > 0) {
                    Queue queue = queueList.get(0);
                    String serviceType = queue.getStr("SERVICE_TYPE");
                    String subServiceName = serviceNameMap.get(serviceType);
                    JSONObject queueJson = JSONObject.parseObject(queue.toJson());
                    queueJson.put("SUB_SERVICE_NAME", subServiceName);
                    opJson.put("QUEUE", queueJson);
                }
            }
            result.add(opJson);
        }
        renderJson(result.toJSONString());
    }

    /**
     * 获取号码
     */
    public void getNumber() {
        HttpServletRequest request = this.getRequest();
        String serviceType = request.getParameter("serviceType");
        String nextServiceType = request.getParameter("nextServiceType");
        String realName = request.getParameter("realName");
        String idNumber = request.getParameter("idNumber");

        String idType = request.getParameter("idType");
        String openId = request.getParameter("openId");
        String wxName = request.getParameter("wxName");


        realName = SerializeUtil.encodeBase64(realName);
        idNumber = SerializeUtil.encodeBase64(idNumber);
        wxName = SerializeUtil.encodeBase64(wxName);

        String wxIconUrl = request.getParameter("wxIconUrl");
        String serviceZone = serviceZoneMap.get(serviceType);
        String serviceQueue = serviceQueueMap.get(serviceType);
        String serviceTypeName = serviceNameMap.get(serviceType);
        String subServiceTypeName = serviceNameMap.get(nextServiceType);

        JSONObject responseJson = ApiUtil.getNewNumber(realName, idNumber, serviceZone, serviceQueue);
        String responseResult = responseJson.getString("result");
        String responseMessage = responseJson.getString("msg");
        JSONObject result = new JSONObject();
        result.put("result", responseResult);
        result.put("msg", responseMessage);
        if ("OK".equals(responseResult)) {
            JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
            String serviceNumber = resBody.getString("service_number");
            String windowName = resBody.getString("window_name");
            int waitPerson = resBody.getInteger("wait_person");
            Timestamp now = new Timestamp(new Date().getTime());
            String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
            result.put("serviceNumber", serviceNumber);
            result.put("windowName", windowName);
            result.put("waitPerson", waitPerson);
            result.put("subServiceTypeName", subServiceTypeName);
            result.put("serviceTypeName", serviceTypeName);
            Queue queue = new Queue().set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                    .set("QUEUE_NUMBER", serviceNumber)
                    .set("SERVICE_ZONE", serviceZone)
                    .set("SERVICE_TYPE", serviceType)
                    .set("NEXT_SERVICE_TYPE", nextServiceType)
                    .set("IDENTITY_TYPE", idType)
                    .set("IDENTITY_NUMBER", idNumber)
                    .set("REAL_NAME", realName)
                    .set("STATE", "1")
                    .set("CREATE_TIME", now)
                    .set("UPDATE_TIME", now)
                    .set("DAY_ID", dayId)
                    .set("OPEN_ID", openId)
                    .set("WX_NAME", wxName)
                    .set("WINDOW_NAME", windowName)
                    .set("WX_ICON_URL", wxIconUrl)
                    .set("WAIT_PERSON", waitPerson);
            queue.save();

            sendNumberMessage(queue);
        } else {

        }
        renderJson(result.toJSONString());
    }

    /**
     * 调用视瀚叫号接口
     */
    public void callNumber() {
        JSONObject responseJson = ApiUtil.callNumber();
        String responseResult = responseJson.getString("result");
        String responseMessage = responseJson.getString("msg");
        JSONObject result = new JSONObject();
        result.put("result", responseResult);
        result.put("msg", responseMessage);
        if ("OK".equals(responseResult)) {
            if (responseJson.getJSONArray("ResBody").size() > 0) {
                JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                String serviceNumber = resBody.getString("service_number");
                result.put("service_number", serviceNumber);
                result.put("window_name", resBody.getString("window_name"));
                String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
                List<Queue> queueList = Queue.dao.find("select * from CRJ_QUEUE where DAY_ID = ? and QUEUE_NUMBER = ?", dayId, serviceNumber);
                if (queueList != null && queueList.size() > 0) {
                    queueList.get(0).set("STATE", QUEUE_STATE_PERFORM).update();
                }
            }
        }else
        {
            if(responseMessage.indexOf("有未完成的呼叫")>-1)
            {
                Queue currentNumber = Queue.getCurrentNo("Z");
                if (currentNumber!=null)
                {
                    String no = currentNumber.getStr("QUEUE_NUMBER");
                    System.out.println("-------------->" +no);
                    responseJson = ApiUtil.recallNumber(no);
                    responseResult = responseJson.getString("result");
                    responseMessage = responseJson.getString("msg");
                    result.put("result", responseResult);
                    result.put("msg", responseMessage);
                }
            }
        }
        renderJson(result.toJSONString());
    }


    /**
     * 内部调用
     */
    public void modifyNumber() {
        HttpServletRequest request = this.getRequest();
        String serviceNumber = request.getParameter("no");
        String state = request.getParameter("state");
        Queue queue = Queue.getQueueByNumber(serviceNumber);
        JSONObject result = new JSONObject();
        JSONObject responseJson = ApiUtil.updateNumberState(serviceNumber, state);
        String responseResult = responseJson.getString("result");
        String responseMessage = responseJson.getString("msg");
        if (queue != null) {
            if ("OK".equals(responseResult)) {
                queue.set("STATE", state).update();
                result.put("result", "OK");
            } else {
                result.put("result", "ERROR");
                result.put("msg", responseMessage);
            }
        } else {
            result.put("result", "ERROR");
            result.put("msg", "无此号码");
        }
        renderJson(result.toJSONString());
    }


    /**
     * 挂起处理
     */
    public void hangupRecover() {
        String serviceNumber = getPara("no");
        Queue queue = Queue.getQueueByNumber(serviceNumber);
        JSONObject result = new JSONObject();
        if (queue != null) {
            if (queue.getStr("STATE").equals("5")) {
                String serviceZone = queue.getStr("SERVICE_ZONE");
                int timeLimit = 30;
                if ("Z".equals(serviceZone)) {
                    Dictionary dictionary = Dictionary.getDictionaryByName("TIME_LIMIT1");
                    if (dictionary != null) {
                        timeLimit = Integer.parseInt(dictionary.getStr("DICT_VALUE"));
                    }
                } else {
                    Dictionary dictionary = Dictionary.getDictionaryByName("TIME_LIMIT2");
                    if (dictionary != null) {
                        timeLimit = Integer.parseInt(dictionary.getStr("DICT_VALUE"));
                    }
                }
                Timestamp now = new Timestamp(new Date().getTime());
                long createTime = queue.getTimestamp("UPDATE_TIME").getTime();
                long nowTime = new Date().getTime();
                if (nowTime - createTime <= timeLimit * 60 * 1000l) {
                    JSONObject responseJson = ApiUtil.updateNumberState(serviceNumber, "1");
                    String responseResult = responseJson.getString("result");
                    String responseMessage = responseJson.getString("msg");
                    if ("OK".equals(responseResult)) {
                        result.put("result", "OK");
                        result.put("msg", responseMessage);
                        queue.set("STATE", "1")
                                .set("UPDATE_TIME", now)
                                .update();

                        String openId = queue.getStr("OPEN_ID");
                        if (openId != null) {
                            //推送挂起恢复消息
                        }

                    } else {
                        result = responseJson;
                    }
                } else {
                    String idNumber = queue.getStr("IDENTITY_NUMBER");
                    String realName = queue.getStr("REAL_NAME");
                    String serviceType = queue.getStr("SERVICE_TYPE");
                    String serviceQueue = serviceQueueMap.get(serviceType);
                    JSONObject responseJson = ApiUtil.getNewNumber(realName, idNumber, serviceZone, serviceQueue);

                    String responseResult = responseJson.getString("result");
                    String responseMessage = responseJson.getString("msg");
                    if ("OK".equals(responseResult)) {
                        result.put("result", "NEW");
                        result.put("msg", responseMessage);
                        queue.set("STATE", "4").set("UPDATE_TIME", now).update();

                        JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                        String newServiceNumber = resBody.getString("service_number");
                        String windowName = resBody.getString("window_name");
                        int waitPerson = resBody.getInteger("wait_person");
                        queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                                .set("QUEUE_NUMBER", newServiceNumber)
                                .set("STATE", "1")
                                .set("CREATE_TIME", now)
                                .set("UPDATE_TIME", now)
                                .set("WINDOW_NAME", windowName)
                                .set("WAIT_PERSON", waitPerson)
                                .save();
                        String openId = queue.getStr("OPEN_ID");
                        if (openId != null) {
                            sendNumberMessage(queue);
                        }
                        result.put("QUEUE", JSONObject.parse(queue.toJson()));
                    } else {
                        result = responseJson;
                    }
                }
            } else {
                result.put("result", "ERROR");
                result.put("msg", "当前号码未处于挂起状态，无法进行恢复操作。");
            }
        } else {
            result.put("result", "ERROR");
            result.put("msg", "无此号码，无法进行恢复操作。");
        }
        renderJson(result.toJSONString());
    }

    /**
     * 供视瀚调用
     */
    public void changeState() {
        String serviceNumber = getPara("service_number");
        String state = getPara("service_state");
        String policeNo = getPara("police_no");
        Queue queue = Queue.getQueueByNumber(serviceNumber);
        JSONObject jsonResult = new JSONObject();
        if (queue != null) {
            if (policeNo != null) {
                queue.set("POLICE_NO", policeNo);
            }
            queue.set("STATE", state).update();
            String serviceType = queue.getStr("SERVICE_TYPE");
            String openId = queue.getStr("OPEN_ID");
            String serviceTypeName = serviceNameMap.get(serviceType);
            String time = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(queue.getTimestamp("CREATE_TIME"));
            String serviceZone = serviceZoneMap.get(serviceType);
            String window = queue.getStr("WINDOW_NAME");
            String waitPerson = "1";
            String dayId = queue.getStr("DAY_ID");
            String url = WEB_URL + "crj/pn?no=" + serviceNumber + "&d=" + dayId;

            if (openId != null) {
                if (QUEUE_STATE_WAIT.equals(state)) {
                    if (serviceNumber.indexOf("V") > -1) {
                        String json = TemplateData.New().setTemplate_id(RECOVER_TEMPLATE_VIP)
                                .setTouser(openId)
                                .setUrl(url)
                                .add("serviceNumber", serviceNumber, "#0000FF")
                                .add("serviceName", serviceTypeName, "#404040")
                                .add("time", time, "#404040")
                                .add("waitPerson", "" + waitPerson, "#404040")
                                .add("window", window, "#0000FF")
                                .build();
                        TemplateMsgApi.send(json);
                    } else {

                        String json = TemplateData.New().setTemplate_id(RECOVER_TEMPLATE)
                                .setTouser(openId)
                                .setUrl(url)
                                .add("serviceNumber", serviceNumber, "#0000FF")
                                .add("serviceName", serviceTypeName, "#404040")
                                .add("time", time, "#404040")
                                .add("waitPerson", "" + waitPerson, "#404040")
                                .add("window", window, "#0000FF")
                                .build();
                        TemplateMsgApi.send(json);
                    }
                }
                if (QUEUE_STATE_PERFORM.equals(state)) {
                    String json = TemplateData.New().setTemplate_id(CALL_TEMPLATE)
                            .setTouser(openId)
                            .add("serviceNumber", serviceNumber, "#0000FF")
                            .add("serviceZone", serviceZone + "区", "#0000FF")
                            .build();
                    TemplateMsgApi.send(json);
                }
                if (QUEUE_STATE_HANGUG.equals(state)) {
                    String json = TemplateData.New().setTemplate_id(HANGUP_TEMPLATE)
                            .setTouser(openId)
                            .add("serviceNumber", serviceNumber, "#0000FF")
                            .build();
                    TemplateMsgApi.send(json);
                }
            }


            jsonResult.put("result", "OK");
        } else {
            jsonResult.put("result", "ERROR");
            jsonResult.put("msg", "无此号码");
        }
        renderJson(jsonResult.toJSONString());
    }


    public void vip() {
        HttpServletRequest request = this.getRequest();
        String serviceNumber = request.getParameter("no");
        String vipType = request.getParameter("vip_type");
        Queue queue = Queue.getQueueByNumber(serviceNumber);
        JSONObject result = new JSONObject();
        if (queue != null) {
            String state = queue.getStr("STATE");
            if ("1".equals(state) || "5".equals(state)) {
                String serviceType = queue.getStr("SERVICE_TYPE");
                String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
                if (serviceType.indexOf("V") == 0) {
                    result.put("result", "ERROR");
                    result.put("msg", "此号码已经为VIP号");
                } else {
                    serviceType = "V" + serviceType;

                    if (nextServiceType != null && nextServiceType.length() > 0) {
                        nextServiceType = "V" + nextServiceType;
                    }

                    String realName = queue.getStr("REAL_NAME");
                    String identityNumber = queue.getStr("IDENTITY_NUMBER");
                    String serviceZone = queue.getStr("SERVICE_ZONE");
                    String serviceQueue = serviceQueueMap.get(serviceType);

                    JSONObject responseJson = ApiUtil.getNewNumber(realName, identityNumber, serviceZone, serviceQueue);
                    String responseResult = responseJson.getString("result");
                    if ("OK".equals(responseResult)) {
                        queue.set("STATE", QUEUE_STATE_PASS).update();
                        JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                        String newServiceNumber = resBody.getString("service_number");
                        Timestamp now = new Timestamp(new Date().getTime());
                        queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                                .set("QUEUE_NUMBER", newServiceNumber)
                                .set("SERVICE_ZONE", serviceZone)
                                .set("SERVICE_TYPE", serviceType)
                                .set("NEXT_SERVICE_TYPE", nextServiceType)
                                .set("STATE", "1")
                                .set("CREATE_TIME", now)
                                .set("VIP_TYPE", vipType)
                                .set("UPDATE_TIME", now).save();
                        sendNumberMessage(queue);
                        result = JSONObject.parseObject(queue.toJson());
                        result.put("result", "OK");
                    } else {
                        result = responseJson;
                    }
                }

            } else if ("2".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码正在办理");
            } else if ("3".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已办理完成");
            } else if ("4".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已失效");
            }
        } else {
            result.put("result", "ERROR");
            result.put("msg", "无此号码");
        }
        renderJson(result.toJSONString());
    }

    public void getNextNumber() {
        HttpServletRequest request = this.getRequest();
        String serviceNumber = request.getParameter("no");
        JSONObject result = new JSONObject();

        Queue queue = Queue.getQueueByNumber(serviceNumber);
        if (queue != null) {
            String state = queue.getStr("STATE");
            String tableState = queue.getStr("TABLE_STATE");
            if ("3".equals(state)) {
                String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
                String serviceZone = serviceZoneMap.get(nextServiceType);
                String serviceQueue = serviceQueueMap.get(nextServiceType);

                String realName = queue.getStr("REAL_NAME");
                String identityNumber = queue.getStr("IDENTITY_NUMBER");

                JSONObject responseJson = ApiUtil.getNewNumber(realName, identityNumber, serviceZone, serviceQueue);
                String responseResult = responseJson.getString("result");

                if ("OK".equals(responseResult)) {
                    queue.set("STATE", QUEUE_STATE_NEXT).update();

                    JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                    String newServiceNumber = resBody.getString("service_number");
                    int waitPerson = resBody.getInteger("wait_person");
                    String windowName = resBody.getString("window_name");
                    Timestamp now = new Timestamp(new Date().getTime());
                    String serviceTypeName = serviceNameMap.get(nextServiceType);
                    queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                            .set("QUEUE_NUMBER", newServiceNumber)
                            .set("SERVICE_ZONE", serviceZone)
                            .set("SERVICE_TYPE", nextServiceType)
                            .set("NEXT_SERVICE_TYPE", null)
                            .set("STATE", "1")
                            .set("WAIT_PERSON", waitPerson)
                            .set("WINDOW_NAME", windowName)
                            .set("CREATE_TIME", now)
                            .set("UPDATE_TIME", now).save();

                    result = JSONObject.parseObject(queue.toJson());
                    result.put("SERVICE_TYPE_NAME", serviceTypeName);
                    result.put("result", "OK");
                } else {

                }


            } else if ("2".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码正在办理");
            } else if ("3".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已办理完成");
            } else if ("4".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已失效");
            } else if ("5".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码处于挂起状态");
            }
        } else

        {
            result.put("result", "ERROR");
            result.put("msg", "无此号码");
        }

        renderJson(result.toJSONString());
    }


    private static void sendNumberMessage(Queue queue) {
        String templateId = null;
        String openId = queue.getStr("OPEN_ID");
        if (openId != null) {
            String serviceZone = queue.getStr("SERVICE_ZONE");
            String serviceNumber = queue.getStr("QUEUE_NUMBER");
            String dayId = queue.getStr("DAY_ID");
            String time = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(queue.getTimestamp("CREATE_TIME"));
            int waitPerson = queue.getInt("WAIT_PERSON");
            String window = "";
            String serviceType = queue.getStr("SERVICE_TYPE");
            String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
            String url = null;
            String serviceTypeName = serviceNameMap.get(serviceType);
            if ("Z".equals(serviceZone)) {
                if (serviceNumber.indexOf("V") > -1) {
                    templateId = PHOTO_MESSAGE_TEMPLATE_VIP;
                } else {
                    templateId = PHOTO_MESSAGE_TEMPLATE;
                }
                url = WEB_URL + "crj/pn?no=" + serviceNumber + "&d=" + dayId;
                window = "自助照相区";
            } else {
                if (serviceNumber.indexOf("V") > -1) {
                    templateId = SERVICE_MESSAGE_TEMPLATE_VIP;
                } else {
                    templateId = SERVICE_MESSAGE_TEMPLATE;
                }
                url = WEB_URL + "crj/pn?no=" + serviceNumber + "&d=" + dayId;
                window = "业务" + serviceZone + "区";
            }
            String json = TemplateData.New().setTemplate_id(templateId)
                    .setTouser(openId)
                    .setUrl(url)
                    .add("serviceNumber", serviceNumber, "#0000FF")
                    .add("serviceName", serviceTypeName, "#404040")
                    .add("time", time, "#404040")
                    .add("waitPerson", "" + waitPerson, "#404040")
                    .add("window", window, "#0000FF")
                    .build();
            TemplateMsgApi.send(json);
        }
//        else {
//            String serviceType = queue.getStr("SERVICE_TYPE");
//            if (serviceType.indexOf("A") > -1) {
//                String mId = "1";
//                String op = "3";
//                String serviceNumber = queue.getStr("QUEUE_NUMBER");
//                new MachineOperation().set("OP_ID", "SEQ_OP_ID.nextval").set("MACHINE_ID", mId).set("OP_TYPE", op).set("OP_PARAMS", serviceNumber).set("STATE", "0").save();
//            }
//        }

    }

    private static void printNumberMessage(Queue queue, String clientNo) {

        String serviceType = queue.getStr("SERVICE_TYPE");
        if (serviceType.indexOf("A") > -1) {
            String mId = "1";
            String op = "3";
            String serviceNumber = queue.getStr("QUEUE_NUMBER");
            new MachineOperation().set("OP_ID", "SEQ_OP_ID.nextval").set("MACHINE_ID", mId).set("OP_TYPE", op).set("OP_PARAMS", serviceNumber).set("STATE", "0").save();
        }


    }


    /**
     * 照相号码
     */
    public void pn() {
        String number = getPara("no");
        String dayId = getPara("d");
        String nowDayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        if (nowDayId.equals(dayId)) {
            List<Queue> queueList = Queue.dao.find("select * from CRJ_QUEUE q where q.QUEUE_NUMBER = ? and q.DAY_ID = ?", number, dayId);
            if (queueList != null && queueList.size() > 0) {
                Queue queue = queueList.get(0);
                String timeString = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(queue.getTimestamp("CREATE_TIME"));
                String serviceType = queue.getStr("SERVICE_TYPE");
                String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
                String state = queue.get("STATE");

                String numberTips = "";
                boolean isPhoto = true;
                if ("1".equals(state) || "2".equals(state)) {
                    if (serviceType.indexOf("Z") > -1) {
                        isPhoto = true;
                        if (nextServiceType != null && nextServiceType.indexOf("A") > -1)  //需要照相并填表的业务
                        {
                            numberTips = "照相并填表完成后";
                        } else {      //仅需要照相的业务
                            numberTips = "照相完成后";
                        }
                    } else {
                        isPhoto = false;
                    }
                    String serviceTypeName = serviceNameMap.get(queue.getStr("SERVICE_TYPE"));
                    setAttr("timeString", timeString);
                    setAttr("serviceTypeName", serviceTypeName);
                    setAttr("queue", queue);
                    setAttr("isPhoto", isPhoto);
                    setAttr("tips", numberTips);

                    if (isPhoto) {
                        setAttr("button_display", "block");
                    } else {
                        setAttr("button_display", "none");
                    }
                    renderTemplate("/view/photoNumber.html");
                } else {
                    setAttr("tips", "此号码已失效，如有疑问请咨询大厅工作人员。");
                    renderTemplate("/view/tips.html");
                }
            } else {
                setAttr("tips", "系统出错，无此号码。");
                renderTemplate("/view/tips.html");
            }
        } else {
            setAttr("tips", "此号码已过期，如有疑问请咨询大厅工作人员。");
            renderTemplate("/view/tips.html");
        }
    }


    /**
     * 获取下一个号码
     */
    public void gnn() {
        String number = getPara("cn");
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        JSONObject result = new JSONObject();
        Queue queue = Queue.getQueueByNumber(number, dayId);
        if (queue != null) {
            String state = queue.getStr("STATE");
            String tableState = queue.getStr("TABLE_STATE");
            if ("3".equals(state)) {
                String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
                String serviceZone = serviceZoneMap.get(nextServiceType);
                String serviceQueue = serviceQueueMap.get(nextServiceType);

                String realName = queue.getStr("REAL_NAME");
                String identityNumber = queue.getStr("IDENTITY_NUMBER");


                JSONObject responseJson = ApiUtil.getNewNumber(realName, identityNumber, serviceZone, serviceQueue);
                String responseResult = responseJson.getString("result");
                String responseMsg = responseJson.getString("msg");

                if ("OK".equals(responseResult)) {
                    queue.set("STATE", QUEUE_STATE_NEXT).update();
                    JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                    String newServiceNumber = resBody.getString("service_number");
                    int waitPerson = resBody.getInteger("wait_person");
                    String windowName = resBody.getString("window_name");
                    Timestamp now = new Timestamp(new Date().getTime());
                    queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                            .set("QUEUE_NUMBER", newServiceNumber)
                            .set("SERVICE_ZONE", serviceZone)
                            .set("SERVICE_TYPE", nextServiceType)
                            .set("NEXT_SERVICE_TYPE", null)
                            .set("STATE", "1")
                            .set("WAIT_PERSON", waitPerson)
                            .set("WINDOW_NAME", windowName)
                            .set("CREATE_TIME", now)
                            .set("UPDATE_TIME", now).save();

                    sendNumberMessage(queue);
                    result.put("result", "OK");
                    result.put("nn", newServiceNumber);
                    result.put("di", dayId);

                } else {
                    result.put("result", "ERROR");
                    result.put("msg", responseMsg);
                }


            } else if ("2".equals(state) || "1".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码正在办理,无法获取新号码。");

            } else if ("4".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已失效，无法获取新号码");

            } else if ("5".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码处于挂起状态，无法获取新号码");
            } else if ("6".equals(state)) {
                result.put("result", "ERROR");
                result.put("msg", "此号码已获取过业务号码，无法重复获取新号码。");
            }

        } else {
            result.put("result", "ERROR");
            result.put("msg", "系统出错，无此号码");
        }
        renderJson(result.toJSONString());
    }


    public void validatePwd() {

        String input = getPara("pwd");
        Dictionary dictionary = Dictionary.getDictionaryByName("PASSWORD");
        if (dictionary != null) {
            String password = dictionary.getStr("DICT_VALUE");
            if (Md5Util.getPwd(input).equals(password)) {
                renderJson("{result:'OK'}");
            } else {
                renderJson("{result:'ERROR'}");
            }
        } else {
            renderJson("{result:'ERROR'}");

        }
    }

    public void sendNext() {
        String mId = getPara("mId");
        String number = getPara("no");
        JSONObject result = new JSONObject();
        Queue queue = Queue.getQueueByNumber(number);
        if (queue != null) {
            String state = queue.getStr("STATE");
            String tableState = queue.getStr("TABLE_STATE");
            String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
            if (nextServiceType != null && !nextServiceType.equals("")) {
                String serviceZone = serviceZoneMap.get(nextServiceType);
                String serviceQueue = serviceQueueMap.get(nextServiceType);

                String realName = queue.getStr("REAL_NAME");
                String identityNumber = queue.getStr("IDENTITY_NUMBER");

                JSONObject responseJson = ApiUtil.getNewNumber(realName, identityNumber, serviceZone, serviceQueue);
                String responseResult = responseJson.getString("result");
                String responseMsg = responseJson.getString("msg");

                if ("OK".equals(responseResult)) {
                    queue.set("STATE", QUEUE_STATE_NEXT).update();
                    JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                    String newServiceNumber = resBody.getString("service_number");
                    int waitPerson = resBody.getInteger("wait_person");
                    String windowName = resBody.getString("window_name");
                    Timestamp now = new Timestamp(new Date().getTime());
                    queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                            .set("QUEUE_NUMBER", newServiceNumber)
                            .set("SERVICE_ZONE", serviceZone)
                            .set("SERVICE_TYPE", nextServiceType)
                            .set("NEXT_SERVICE_TYPE", null)
                            .set("STATE", "1")
                            .set("WAIT_PERSON", waitPerson)
                            .set("WINDOW_NAME", windowName)
                            .set("CREATE_TIME", now)
                            .set("UPDATE_TIME", now).save();

                    sendNumberMessage(queue);
                    result.put("result", "OK");
                    result.put("newNumber", newServiceNumber);
                    String openId = queue.getStr("OPEN_ID");
                    if (openId != null) {
                        sendNumberMessage(queue);
                    } else {
                        new MachineOperation().set("OP_ID", "SEQ_OP_ID.nextval").set("MACHINE_ID", mId).set("OP_TYPE", "3").set("OP_PARAMS", newServiceNumber).set("STATE", "0").save();
                    }


                } else {
                    result.put("result", "ERROR");
                    result.put("msg", responseMsg);
                }
            } else {

                result.put("result", "ERROR");
                result.put("msg", "此号码无后续业务");
            }
        } else {
            result.put("result", "ERROR");
            result.put("msg", "系统出错，无此号码");
        }
        renderJson(result.toJSONString());
    }


    /**
     * 填表完成
     *
     * @param idNumber 证件号码
     */
    public static void submitTable(String idNumber, String clientNo) {
        idNumber = SerializeUtil.encodeBase64(idNumber);
        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<Queue> queueList = Queue.dao.find("select * from CRJ_QUEUE where DAY_ID = ? and IDENTITY_NUMBER = ? and STATE = '3'", dayId, idNumber);
        if (queueList != null && queueList.size() > 0) {
            Queue queue = queueList.get(0);
            String nextServiceType = queue.getStr("NEXT_SERVICE_TYPE");
            String serviceZone = serviceZoneMap.get(nextServiceType);
            String serviceQueue = serviceQueueMap.get(nextServiceType);

            String realName = queue.getStr("REAL_NAME");
            String identityNumber = queue.getStr("IDENTITY_NUMBER");


            JSONObject responseJson = ApiUtil.getNewNumber(realName, identityNumber, serviceZone, serviceQueue);
            String responseResult = responseJson.getString("result");

            if ("OK".equals(responseResult)) {
                queue.set("STATE", QUEUE_STATE_NEXT).update();

                JSONObject resBody = responseJson.getJSONArray("ResBody").getJSONObject(0);
                String newServiceNumber = resBody.getString("service_number");
                int waitPerson = resBody.getInteger("wait_person");
                String windowName = resBody.getString("window_name");
                Timestamp now = new Timestamp(new Date().getTime());
                queue.set("QUEUE_ID", "SEQ_QUEUE_ID.nextval")
                        .set("QUEUE_NUMBER", newServiceNumber)
                        .set("SERVICE_ZONE", serviceZone)
                        .set("SERVICE_TYPE", nextServiceType)
                        .set("NEXT_SERVICE_TYPE", null)
                        .set("STATE", "1")
                        .set("WAIT_PERSON", waitPerson)
                        .set("WINDOW_NAME", windowName)
                        .set("CREATE_TIME", now)
                        .set("UPDATE_TIME", now).save();
                sendNumberMessage(queue);

            }
        }
    }

    public void updateMachineState() {
        String mId = getPara("mId");
        String state = getPara("state");
        Timestamp now = new Timestamp(new Date().getTime());
        Machine machine = Machine.dao.findById(mId);
        machine.set("STATE", state).set("START_TIME", now).update();
        renderJson("{\"result\":\"ok\"}");
    }

    public void yttq()
    {


        HttpClient client = HttpClients.createDefault();

        try {
            HttpGet request = new HttpGet("http://jisutqybmf.market.alicloudapi.com/weather/query?city=烟台");
            request.setHeader("Authorization","APPCODE f77530e7d1324c3bbd47c819aa33711c");
            HttpResponse response = client.execute(request);
            String responseString = EntityUtils.toString(response.getEntity());
            renderJson(responseString);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


}


